//蓝牙控制相关

#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <Wire.h>
#include <WiFi.h>
#include <BleMouse.h>
//#include <BleKeyboard.h>

//BleKeyboard bleKeyboard("epaperwatch", "dht", 100);
BleMouse bleMouse("epaperwatch", "dht", 100);
float roollength = 10;
// void Bkeyboard() {
//   WiFi.mode(WIFI_OFF);
//   //int bletry=0;
//   //do {
//   bleKeyboard.begin();
//   //if(tapcheck(2))break;
//   //}
//   //while (!bleKeyboard.isConnected());
//   while (1) {
//     if (bleKeyboard.isConnected()) {
//       Serial.println("bleok");
//       while (1) {
//         if (tapcheck(1)) {
//           bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
//           Serial.println("KEY_MEDIA_NEXT_TRACK");
//           delay(200);
//           continue;
//         }
//         if (tapcheck(3)) {
//           bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
//           Serial.println("KEY_MEDIA_PREVIOUS_TRACK");
//           delay(200);
//           continue;
//         }
//         if (tapcheck(2)) {
//           bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
//           Serial.println("KEY_MEDIA_VOLUME_UP");
//           delay(200);
//           continue;
//         }
//       }
//     }
//   }
// }
void slip(int mod) {
  unsigned long startTime;
  Serial.println("按住左键");

  if (mod == 0) {
    bleMouse.release(MOUSE_LEFT);
    bleMouse.move(0, 500);
    bleMouse.press(MOUSE_LEFT);
    delay(220);
    Serial.println("Move mouse pointer up");
    startTime = millis();
    while (millis() < startTime + 100) {
      bleMouse.move(0, -50);
      delay(10);
    }
    Serial.println("释放左键");
    bleMouse.release(MOUSE_LEFT);
    delay(220);
    Serial.println("Move mouse pointer down");
    startTime = millis();
    while (millis() < startTime + 100) {
      bleMouse.move(0, 50);
      delay(10);
    }
  } else if (mod == 1) {
    bleMouse.release(MOUSE_LEFT);
    bleMouse.move(0, -500);
    bleMouse.press(MOUSE_LEFT);
    delay(220);
    Serial.println("Move mouse pointer up");
    startTime = millis();
    while (millis() < startTime + 100) {
      bleMouse.move(0, +50);
      delay(10);
    }
    Serial.println("释放左键");
    bleMouse.release(MOUSE_LEFT);
    delay(220);
    Serial.println("Move mouse pointer down");
    startTime = millis();
    while (millis() < startTime + 100) {
      bleMouse.move(0, -50);
      delay(10);
    }
  }
}
void mouseloop() {
  //display.fillScreen(FormC);
  //display.nextPage();
  int tap = 0;
  //SPI.end();
  while (1) {
    //if (tapcheck(1) && tapcheck(2) && tapcheck(3)) ESP.restart();  //模拟rst
    //if (bleMouse.isConnected()) {
    //delay(100);
    //if(digitalRead(9)==LOW)slip(1);
    tap = 0;
    sensors_event_t a, g, temp;
    mpu.getEvent(&a, &g, &temp);
    if (g.gyro.y >= 5 && abs(g.gyro.x) < 2) {  //slip(1);
      int state6050 = 0;
      int tick6050 = 0;
      while (tick6050 < 20) {
        mpu.getEvent(&a, &g, &temp);
        if ((g.gyro.y < 1 || g.gyro.y > -2) && abs(g.gyro.x) < 0.6) {
          delay(30);
          if ((g.gyro.y < 0.5 || g.gyro.y > -1) && abs(g.gyro.x) < 0.6) {
            state6050 = 1;
            break;
          }
        }
      tick6050++;
      delay(10);
      }
      if (state6050 == 1) {
        slip(1);
        delay(400);
        continue;
      } else delay(200);
    }

    if (g.gyro.y <= -5 && abs(g.gyro.x) <= 2) {  //slip(1);
      int state6050 = 0;
      int tick6050 = 0;
      while (tick6050 < 20) {
        mpu.getEvent(&a, &g, &temp);
        if ((g.gyro.y > -1 || g.gyro.y < 2) && abs(g.gyro.x) < 0.6) {
          delay(30);
          if ((g.gyro.y > -0.5 || g.gyro.y < 1) && abs(g.gyro.x) < 0.6) {
            state6050 = 2;
            break;
          }
        }
        tick6050++;
        delay(10);
      }
      if (state6050 == 2) {
        slip(0);
        delay(500);
        continue;
      }
      delay(200);
    }
    if (abs(g.gyro.x) >= 7 && abs(g.gyro.y) < 2) {
      int tick6050 = 0;
      while (tick6050 < 20) {
        mpu.getEvent(&a, &g, &temp);
        if (abs(g.gyro.y) <= 1&& abs(g.gyro.x) < 2) {
          delay(30);
          if (abs(g.gyro.y) < 0.8&& abs(g.gyro.x) <= 1) {
            bleMouse.click(MOUSE_LEFT);
            break;
          }
        }
        tick6050++;
        delay(10);
      }
      
      delay(500);
      continue;
    }
    if (tapcheck(2)) break;
  }
}
// }
// void mouseloop() {
//   display.fillScreen(FormC);
//   display.nextPage();
//   int tap = 0;
//   while (1) {
//     if (tapcheck(1) && tapcheck(2) && tapcheck(3)) ESP.restart();  //模拟rst
//     if (bleMouse.isConnected()) {
//       delay(100);
//       tap = 0;
//       if (!tapcheck(2) && tapcheck(1) && tap == 0) {

//         tap = 1;
//         slip(1);
//         delay(20);
//         continue;
//       }
//       if (!tapcheck(2) && tapcheck(3) && tap == 0) {

//         tap = 1;
//         slip(0);
//         delay(20);
//         continue;
//       }
//       if (!tapcheck(1) && !tapcheck(3) && tapcheck(2) && tap == 0) {
//         Serial.println("暂停");
//         tap = 1;
//         bleMouse.click();

//         delay(100);
//         continue;
//       }
//       if (((tapcheck(1) && tapcheck(2)) || (tapcheck(2) && tapcheck(3))) && tap == 0) {
//         return;
//       }
//       if (tapcheck(1) && tapcheck(2) && tapcheck(3)) ESP.restart();  //模拟rst
//     }
//   }
// }
extern void mpu_init();
void mousesetup() {
  SPI.end();
  bleMouse.begin();
  WiFi.mode(WIFI_OFF);
  Wire.begin(8, 7);
  mpu.begin();
  mpu.setGyroRange(MPU6050_RANGE_1000_DEG);
  mpu.setHighPassFilter(MPU6050_HIGHPASS_0_63_HZ);
  pinMode(5, INPUT_PULLUP);
  mouseloop();
  mpu_init();
  Wire.end();
  SPI.begin(7, 8, 8, 6);
  esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0);
  gpio_wakeup_enable(GPIO_NUM_18, GPIO_INTR_LOW_LEVEL);
  gpio_wakeup_enable(GPIO_NUM_19, GPIO_INTR_LOW_LEVEL);
  gpio_wakeup_enable(GPIO_NUM_9, GPIO_INTR_LOW_LEVEL);
  gpio_wakeup_enable(GPIO_NUM_4, GPIO_INTR_ANYEDGE);
  gpio_wakeup_enable(GPIO_NUM_4, GPIO_INTR_LOW_LEVEL);
  gpio_wakeup_enable(GPIO_NUM_4, GPIO_INTR_NEGEDGE);
}
